package org.aeon.colorfulplanebattle.flying.thread;

import org.aeon.colorfulplanebattle.flying.entity.Bullet;

import java.util.List;

/**
 * @author: MissingAeon
 * @date: 2024/1/9 15:47
 * @description: 子弹线程
 */
public class BulletThread extends Thread {
    private final List<Bullet> bullets;
    private boolean flag = true;

    public BulletThread(List<Bullet> bullets) {
        this.setName("子弹线程");
        this.bullets = bullets;
    }

    @Override
    public void run() {
        int i, len;
        while (flag) {
            len = bullets.size();
            for (i = 0; i < len; i++) {
                try {
                    Bullet bullet = bullets.get(i);
                    if (!bullet.move(null)) {
                        bullet.setDestroy(true);
                    }
                } catch (IndexOutOfBoundsException e) {
                    break;
                }
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
            }
        }
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
